home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / genctxt.zip / CHAP5.TXT < prev    next >
Text File  |  1987-11-21  |  34KB  |  785 lines

  1.  
  2.             Chapter 5 - Functions, variables, and prototypes
  3.  
  4.  
  5.                       OUR FIRST USER DEFINED FUNCTION
  6.  
  7.              Load  and examine the file SUMSQRES.C for an example of
  8.         a C program with functions.   Actually this is not the first
  9.         function  we have encountered because the "main" program  we
  10.         have been using all along is technically a function,  as  is
  11.         the  "printf" function.   The "printf" function is a library
  12.         function that was supplied with your compiler.
  13.  
  14.              Notice the executable part of this program which begins
  15.         in  line  8.   It  begins  with  a  line  that  simply  says
  16.         "header()",  which  is the way to call  any  function.   The
  17.         parentheses are required because the C compiler uses them to
  18.         determine  that  it  is a function call  and  not  simply  a
  19.         misplaced variable.  When the program comes to this line  of
  20.         code, the function named "header" is called, its  statements
  21.         are executed, and control returns to the statement following
  22.         this call.  Continuing on we come to a "for" loop which will
  23.         be  executed 7 times and which calls another function  named
  24.         "square" each time through the loop, and finally a  function
  25.         named "ending" will be called and executed.  For the  moment
  26.         ignore  the  "index"  in  the parentheses  of  the  call  to
  27.         "square".  We have seen that this program therefore calls  a
  28.         header, 7 square calls, and an ending. Now we need to define
  29.         the functions.
  30.  
  31.                            DEFINING THE FUNCTIONS
  32.  
  33.              Following the main program you will see another program
  34.         that  follows all of the rules set forth so far for a "main"
  35.         program  except that it is named "header()".   This  is  the
  36.         function which is called from within the main program.  Each
  37.         of  these  statements are executed,  and when they  are  all
  38.         complete, control returns to the main program.
  39.  
  40.              The  first  statement sets the variable "sum" equal  to
  41.         zero because we will use it to accumulate a sum of  squares.
  42.         Since  the  variable  "sum" is defined as  an  integer  type
  43.         variable  prior to the main program,  it is available to  be
  44.         used  in  any of the following functions.   It is  called  a
  45.         "global" variable,  and it's scope is the entire program and
  46.         all  functions.   More  will  be  said about  the  scope  of
  47.         variables near the end of this chapter.  The next  statement
  48.         outputs  a header message to the monitor.   Program  control
  49.         then  returns  to  the  main  program  since  there  are  no
  50.         additional statements to execute in this function.
  51.  
  52.              It should be clear to you that the two executable lines
  53.         from  this  function  could be moved to  the  main  program,
  54.         replacing the header call,  and the program would do exactly
  55.         the same thing that it does as it is now written.  This does
  56.  
  57.  
  58.                                   Page 29
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.             Chapter 5 - Functions, variables, and prototypes
  69.  
  70.  
  71.         not minimize the value of functions,  it merely  illustrates
  72.         the operation of this simple function in a simple way.   You
  73.         will find functions to be very valuable in C programming.
  74.  
  75.                        PASSING A VALUE TO A FUNCTION
  76.  
  77.              Going  back  to the main program,  and the  "for"  loop
  78.         specifically,  we find the new construct from the end of the
  79.         last lesson used in the last part of the "for" loop,  namely
  80.         the  "index++".  You should get used to seeing this, as  you
  81.         will see it a lot in C programs.
  82.  
  83.              In the call to the function "square",  we have an added
  84.         feature, namely the variable "index" within the parentheses.
  85.         This  is  an indication to the compiler that when you go  to
  86.         the function,  you wish to take along the value of index  to
  87.         use in the execution of that function.  Looking ahead at the
  88.         function  "square",  we  find that another variable name  is
  89.         enclosed in its parentheses,  namely the variable  "number".
  90.         This  is  the name we prefer to call the variable passed  to
  91.         the  function when we are in the function.   We can call  it
  92.         anything  we wish as long as it follows the rules of  naming
  93.         an identifier.   Since the function must know what type  the
  94.         variable  is,  it is defined following the function name but
  95.         before the opening brace of the function itself.   Thus, the
  96.         line  containing "int number;" tells the function  that  the
  97.         value  passed to it will be an integer type variable.   With
  98.         all of that out of the way,  we now have the value of  index
  99.         from  the main program passed to the function "square",  but
  100.         renamed "number", and available for use within the function.
  101.         This  is the "classic" style of defining function  variables
  102.         and has been in use since C was originally defined.  A newer
  103.         method is gaining in popularity due to its many benefits and
  104.         will be discussed later in this chapter.
  105.  
  106.              Following the opening brace of the function,  we define
  107.         another  variable "numsq" for use only within  the  function
  108.         itself,  (more  about  that  later)  and  proceed  with  the
  109.         required  calculations.   We set "numsq" equal to the square
  110.         of  number,  then add numsq to the current total  stored  in
  111.         "sum".   Remember  that "sum += numsq" is the same as "sum =
  112.         sum + numsq" from the last lesson.   We print the number and
  113.         its square, and return to the main program.
  114.  
  115.                   MORE ABOUT PASSING A VALUE TO A FUNCTION
  116.  
  117.              When we passed the value of "index" to the function,  a
  118.         little  more  happened  than meets  the  eye.   We  did  not
  119.         actually  pass  the  value  of index  to  the  function,  we
  120.         actually  passed  a  copy of the value.   In  this  way  the
  121.         original value is protected from accidental corruption by  a
  122.  
  123.  
  124.                                   Page 30
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.             Chapter 5 - Functions, variables, and prototypes
  135.  
  136.  
  137.         called  function.   We  could  have  modified  the  variable
  138.         "number" in any way we wished in the function "square",  and
  139.         when we returned to the main program, "index" would not have
  140.         been  modified.   We thus protect the value of a variable in
  141.         the main program from being accidentally corrupted,  but  we
  142.         cannot  return  a value to the main program from a  function
  143.         using this technique.  We will find a well defined method of
  144.         returning  values  to  the main program or  to  any  calling
  145.         function  when we get to arrays and another method  when  we
  146.         get  to pointers.   Until then the only way you will be able
  147.         to  communicate  back to the calling function will  be  with
  148.         global  variables.    We  have  already  hinted  at   global
  149.         variables  above,  and will discuss them in detail later  in
  150.         this chapter.
  151.  
  152.              Continuing  in  the main program,  we come to the  last
  153.         function call, the call to "ending".  This call simply calls
  154.         the last function which has no local variables defined.   It
  155.         prints out a message with the value of "sum" contained in it
  156.         to  end the program.   The program ends by returning to  the
  157.         main  program and finding nothing else to do.   Compile  and
  158.         run this program and observe the output.
  159.  
  160.                         NOW TO CONFESS A LITTLE LIE
  161.  
  162.              I told you a short time ago that the only way to get  a
  163.         value  back to the main program was through use of a  global
  164.         variable,  but  there  is another way which we will  discuss
  165.         after  you load and display the file  named  SQUARES.C.   In
  166.